<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ include file="/WEB-INF/component/commonTagLib.jsp"%>

<!DOCTYPE html>
<html>
<head>
    <title><spring:message code="message.list.DingDanPeiSongMingXi"/></title>
    <%@ include file="/WEB-INF/component/commonCSS.jsp"%>
    <style type="text/css">
        .text-left{
            color:#949494;
        }
        div.group-info {
            margin-top: 10px !important;
            margin-bottom: 0px !important;
        }
        input.innerInput {
            width: 100%;
        }
        div.tablediv {
            width: 920px;
            margin: auto;
            margin-top: 20px;
            margin-bottom: 20px;
            border-top: 1px solid #cecece;
        }
        td.ui-sgcollapsed.sgexpanded>div, td.ui-sgcollapsed.sgcollapsed>div {
            width: 20px !important;
        }
        tr.ui-subgrid>td.subgrid-data {
            border-right: 1px solid #cecece;
            border-bottom: 1px solid #cecece;
        }
        div.tablediv div.ui-jqgrid-bdiv, div.tablediv div.ui-jqgrid-hdiv {
            border-right: 0px none;
            border-bottom: 0px none;
        }
        span.addRow {
            width: 20px;
            display: block;
            margin: auto;
            cursor: pointer;
        }
        td.subgrid-data {
            background-color: #FDF5E6;
        }
    </style>
</head>
<body class="skin-blue sidebar-mini fixed skin-blue-light-frame">    
     <section class="content" style="min-height:inherit;">
        <form id="form" class="form-horizontal"  method="post">
            <input type="hidden" name=orderPackId value="${orderPackId}" />
            <table class="infoBox" style="margin-bottom: 5px;">
              <tr>
               <td width="150px" class="infoBoxleft" style="background:url(${ctx}/lib/img/img.png)  no-repeat 40px 12px;">
                    
               </td>
               <td>
                <div class="box-body" style="background:url(${ctx}/lib/img/infobg.png)  no-repeat right bottom;">   
                  <div class="form-group form-group-xs"  style="margin-top:2px;">
                    <label class="col-sm-2 text-right" for="inputTextAlter1"><spring:message code="message.listForm.DingDanBaoHao"/><spring:message code="message.yanZheng.maoHao"/></label>
                    <div class="col-sm-4 text-left" >
                            ${orderPackId}
                    </div>
                    <label class="col-sm-2 text-right" for="inputTextAlter2"><spring:message code="message.lable.yiLiaoJiGouMingCheng"/><spring:message code="message.yanZheng.maoHao"/></label>
                    <div class="col-sm-4 text-left" id="hospName">
                            ${orderPack.hospName}
                    </div>
                  </div>  
                   <div class="form-group form-group-xs"  style="margin-top:-13px;">
                    <label class="col-sm-2 text-right" for="inputTextAlter1"><spring:message code="message.listForm.YiLiaoJiGouDiQu"/><spring:message code="message.yanZheng.maoHao"/></label>
                    <div class="col-sm-4 text-left" id="areaName">
                            ${orderPack.hospArFullname}
                    </div>
                    <label class="col-sm-2 text-right" for="inputTextAlter2"><spring:message code="message.listForm.CaiGouShiJian"/><spring:message code="message.yanZheng.maoHao"/></label>
                    <div class="col-sm-4 text-left" id="submitDateTime">
                            ${orderPack.submitDatetime}
                    </div>
                  </div>  
                  <div class="form-group form-group-xs" style="margin-top:-13px;">                    
                    <label class="col-sm-2 text-right" for="recpUser"><spring:message code="message.listForm.ShouHuoRen"/><spring:message code="message.yanZheng.maoHao"/></label>
                    <div class="col-sm-4 text-left" id="recpUser">
                         ${orderPack.recpUser}
                    </div>
                    <label class="col-sm-2 text-right" for="recpUserPhone"><spring:message code="message.lable.ShouHuoRenDianHua"/><spring:message code="message.yanZheng.maoHao"/></label>
                    <div class="col-sm-4 text-left" id="recpUserPhone">
                         ${orderPack.recpUserphone}
                    </div>
                </div>
                <div class="form-group form-group-xs" style="margin-top:-13px;margin-bottom:0px;">                    
                    <label class="col-sm-2 text-right" for="recpAddr"><spring:message code="message.listForm.ShouHuoDiZhi"/><spring:message code="message.yanZheng.maoHao"/></label>
                    <div class="col-sm-9 text-left" id="recpAddr">
                         ${orderPack.recpAddr}
                    </div>
                </div>
             </div>
       
        </td>
      </tr>
      </table>
            
            
        </form>
    </section>   
    
    <section class="content">
        <div class=" box box-success">
            <div class="box-body">
                <div class="input">
		    		<div class="textalign1"><spring:message code="message.listForm.tongYongMing"/><spring:message code="message.yanZheng.maoHao"/></div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id="drugName" name="drugName" placeholder="<spring:message code="message.listForm.tongYongMing"/>/<spring:message code="message.listForm.pinYinMa"/>" />
                    </div>
                    <div class="textalign1" style="display: none;"><spring:message code="message.listForm.yaoPinBianMa"/><spring:message code="message.yanZheng.maoHao"/></div>
                    <div class="boxinput" style="display: none;">
                        <input type="text" class="textinput" id="drugCode" name="drugCode"  placeholder="<spring:message code="message.listForm.yaoPinBianMa"/>" />
                    </div>
                    <div class="textalign1"><spring:message code="message.listForm.ShengChanQiYe"/><spring:message code="message.yanZheng.maoHao"/></div>
                    <div class="boxinput">
                         <input type="text" class="textinput" id="prodComp" name="prodComp"  placeholder="<spring:message code="message.listForm.ShengChanQiYe"/>/<spring:message code="message.listForm.pinYinMa"/>" />
                    </div>
					<div class="textalign1" >  
                   <button type="button" onclick="gridSerch();" class="btn btn-success btn-sm"  id="selectDicCompType"><spring:message code="message.button.seachSpacing"/></button>
                    </div>
					<div class="textalign1" style="width: 70px;">
                         <button type="button" onclick="exportExcel();" class="btn btn-success btn-sm">导 出</button>
                    </div>
                </div>
            </div>
        </div>
        
        <table class="jqgrid" id="gridlist"></table>
        <div id="gridpage"></div>
        
         <div class=" box box-warning bottongroup">
            <label id="dispatchDetail" onclick="dispatchDetail()" class="btn btn-primary btn-sm">配 送</label>
            <label id="dispatchDetail" onclick="queHuo()" class="btn btn-primary btn-sm">缺 货</label>
        </div>
        
   </section>
    
   <%@ include file="/WEB-INF/component/commonJS.jsp"%>
   <script type="text/javascript">
   var submitTyp = 0;   //  0未提交，1已提交
   
   //   增加批号行
   function addRow(subTableId) {
	   var rowId = $("#" + subTableId).getGridParam("records");
	   $("#" + subTableId).addRowData(rowId, {});
	   var $td = $("#" + subTableId).find("textarea.confirmRemark").parent("td");
	   $td.attr("rowspan", rowId + 1);
	   var h = $td.height() - 2;
	   $td.children("textarea").css({
		   "height" : h + "px",
		   "min-height" : h + "px"
	   });
   }
   
   //   删除批号行
   function delRow(subTableId, rowId) {
	   var $td = $("#" + subTableId).find("textarea.confirmRemark").parent("td");
	   $td.children("textarea").hide();
	   $("#" + subTableId).delRowData(rowId);
	   var rowId = $("#" + subTableId).getGridParam("records");
       $td.attr("rowspan", rowId);
       var h = $td.height() - 2;
       $td.children("textarea").css({
           "height" : h + "px",
           "min-height" : h + "px"
       });
       $td.children("textarea").show();
   }
   
   //   填写配送信息时自动选中明细
   function selectDetail(rowId) {
	   var rowIds = $("#gridlist").jqGrid("getGridParam", "selarrrow") || [];
	   var exists = false;
	   for (var i=0; i<rowIds.length; i++) {
		   if (rowId == rowIds[i]) {
			   exists = true;
			   break;
		   }
	   }
	   if (!exists) {
		   $("#gridlist").setSelection(rowId);
	   }
   }
   
   //   验收单号
   function getRecpCode() {
	   var date = new Date().format("yyyyMMddhhmmss");
	   var random = Math.ceil(Math.random() * 1000000);
	   return date + random;
   }
   
   $(function () {
	   //绑定回车
	   $('#drugName,#drugCode,#prodComp').bind('keydown', function(event) {
	       if (event.keyCode == "13") {
	           $('#selectDicCompType').click();
	       }
	   });  
	 
	   $("#drugName").focus();//加载页面光标在第一个查询框内
	   
       $("#gridlist").jqGrid({
           url: "${ctx}/orderDispatch/getOrderDispatchDetail.html",
           mtype: "POST",
           postData:{"orderPackId":"${orderPackId}"},
           datatype: "json",
           autowidth: true,
           shrinkToFit: false,
           height: 310,
           colNames: [
                'orderCode',
				'<spring:message code="message.listForm.CaiGouJia"/>',
				'<spring:message code="message.listForm.CaiGouShuLiang"/>',
				'<spring:message code="message.listForm.CaiGouJinE"/>',
				'<spring:message code="message.listForm.yaoPinBianMa"/>',
				'<spring:message code="message.listForm.tongYongMing"/>', 
				'<spring:message code="message.listForm.jiXing"/>',
				'<spring:message code="message.listForm.guiGe"/>',
				'<spring:message code="message.listForm.zhuanHuanXiShu"/>',
				'<spring:message code="message.listForm.baoZhuangDanWei"/>', 
				'<spring:message code="message.listForm.caiZhi"/>',
				'<spring:message code="message.listForm.shengChanQiYe"/>',
				'国药准字',
				'<spring:message code="message.listForm.CaiGouXianJia"/>',
				'最高零售限价',
				'状态',
				'缺货原因'
			],
			colModel: [
                { name: 'orderCode', width: 60, align: "right",sortable:false, hidden:true },
				{ name: 'proPrice', width: 60, align: "right",sortable:false,
					formatter:function(cellvalue, options, row){
					    return formatAmount(cellvalue, options, row);
					}
				},
				{ name: 'proCount', width: 65, align: "center",sortable:false},
				{ name: 'orderMoney', width: 65, align: "right",sortable:false,
					formatter:function(cellvalue, options, row){
					    return formatAmount(cellvalue, options, row);
					}
				},
				{ name: 'drugCode',hidden:true, width: 100, align: "center",sortable:false,
					formatter:function(cellvalue, options, row){
					    // return drugInfoLink(cellvalue);
					    return cellvalue;
					}
				},
				{ name: 'drugName', width: 140, align: "left" ,sortable:false },
				{ name: 'formName', width: 80 , align: "center",sortable:false},
				{ name: 'specName', width: 80, align: "center" ,sortable:false},
				{ name: 'factor', width: 80, align: "center" ,sortable:false},
				{ name: 'unitName', width: 60, align: "center" ,sortable:false},
				{ name: 'materialName',hidden:true, width: 60, align: "center" ,sortable:false},
				{ name: 'prodCompname', width: 260, align: "left",sortable:false,
					formatter:function(cellvalue, options, row){
					    // return compInfoLink(row.prodCompcode, cellvalue, 1);
					    return cellvalue;
					}
				},
				{ name: 'drugMedicine', width: 80, align: "center" ,sortable:false},
				{ name: 'proPriceLimit', width: 60,align:"right",sortable:false, hidden:true,
					formatter:function(cellvalue, options, row){
					    return formatAmount(cellvalue, options, row);
					}
				},
				{ name: 'retailPriceLimit', width: 60,align:"right",sortable:false, hidden:true,
					formatter:function(cellvalue, options, row){
					    return formatAmount(cellvalue, options, row);
					}
				},
				{ name: 'orderStatus', width: 60, align: "center" ,sortable:false},
				{ name: 'confirmRemark',hidden:true, width: 60, align: "center" ,sortable:false}
				
			],
           rowNum: 20,
           rowList: [10, 20, 50, 100],
           rownumbers: true,
           pager: "#gridpage",
           multiselect: true, 
           viewrecords: true,
           gridComplete: function(){
               //序号列宽度自适应（参数为jqgridID）
             //  autoRNWidth("gridlist");
               //数据只有一条时默认选中（参数为jqgridID）
               selectByOneData("gridlist");
               
               $("td.ui-sgcollapsed.sgcollapsed:first").trigger("click");
           }, 
           caption: "待配送药品列表"
                  + "（验收单号：<input type='text' class='headInput' id='recpCode' value='" + getRecpCode() + "' maxlength='20' />）",
           jsonReader: {
               repeatitems: false,
               id: "orderCode"
           },
           subGrid: true,
           subGridOptions: {
        	   reloadOnExpand : false
           },
           subGridRowExpanded: function(subGridId, rowId) {
        	   $("tr.ui-subgrid>td:not(.subgrid-data)").remove();
        	   $("tr.ui-subgrid>td.subgrid-data").attr("colspan", 14);
        	   var rowdata = $("#gridlist").jqGrid('getRowData', rowId);
        	   var confirmRemark = rowdata.confirmRemark;
        	   var subTableId = "subTable_" + rowId;
        	   $("#" + subGridId).append("<table id='" + subTableId + "' class='scroll'></table>");
        	   
        	   $("#" + subTableId).jqGrid({
                   mtype: "POST",
                   datatype: "local",
                   autowidth: true,
                   shrinkToFit: false,
                   height: "auto",
                   colNames: [
                       '<span class="addRow" title="多批号" onclick="addRow(\'' + subTableId + '\')">+</span>',
					   '批次号',
					   '配送数量',
					   '有效期',
					   '备注',
					   '缺货原因'
				   ],
				   colModel: [
						{ name: 'operate', width: 30, align: "center",sortable:false,
						    formatter:function(cellvalue, options, row){
						    	if (options.rowId == 0) {
						    		return "";
						    	}
						        return '<span class="addRow" title="删除" onclick="delRow(\'' + subTableId + '\', \'' + options.rowId + '\')">×</span>';
						    }
						},
						{ name: 'approveCode', width: 100, align: "center",sortable:false,
						    formatter:function(cellvalue, options, row){
						    	return "<input onchange='selectDetail(\""+rowId+"\")' class='gridText approveCode' type='text' maxlength='64' />";
						    }
						},
						{ name: 'delCount', width: 80, align: "center",sortable:false,
						    formatter:function(cellvalue, options, row){
						    	return "<input class='gridText delCount' type='text' maxlength='11' />";
						    }
						},
						{ name: 'credentialDatetime', width: 160, align: "center",sortable:false,
						    formatter:function(cellvalue, options, row){
						    	var curDate = new Date().format("yyyy-MM-dd");
						    	return "<input class='gridText credentialDatetime' type='text' readonly='readonly' data-date-fmt='yyyy-MM-dd' onFocus='WdatePicker({readOnly:true})' value='' />";
						    }
						},
						{ name: 'remarks', width: 170, align: "center",sortable:false,
						    formatter:function(cellvalue, options, row){
						    	return "<input class='gridText remarks' type='text' maxlength='256' />";
						    }
						},
						{ name: 'confirmRemark', width: 173, align: "center",sortable:false,
						    formatter:function(cellvalue, options, row){
						    	if (options.rowId == 0) {
                                    return "<textarea onchange='selectDetail(\""+rowId+"\")' class='gridText confirmRemark' maxlength='256'>"+ confirmRemark +"</textarea>";
                                }
						    	return "";
						    }
						}
                   ],       
                   rownumbers: true,
                   beforeSelectRow: function(rowid, e) {
                       return false;    //  子表格无选中状态
                   }
        	   });
        	   
        	   $("#" + subTableId).addRowData(0, {});
           }
       });
       $("#gridlist").jqGrid('navGrid', '#gridpage', { add: false, edit: false, del: false, search: false, refresh: false });
       $(window).trigger("resize");
       
       $("html").scrollTop(305);
   });
   
   //查询
   function gridSerch() {
       var drugCode = $.trim($("#drugCode").val());
       var drugName = $.trim($("#drugName").val());
       var prodComp = $.trim($("#prodComp").val());
       $("#gridlist").jqGrid('setGridParam',{  
           datatype:'json',
           postData:{
               "drugCode":drugCode,
               "drugName":drugName,
               "compName":prodComp
           },
           page:1
       }).trigger("reloadGrid"); //重新载入  
   }
   
   // 配送
   function dispatchDetail() {
	   if (submitTyp == 1) {
		   $.alert("后台处理中，请勿重复操作！", "warn");
           return;
	   }
	   
	   var recpCode = $.trim($("#recpCode").val());
	   $("#recpCode").val(recpCode);
	   if (recpCode == null || recpCode == "") {
		   $.alert("请输入验收单号！", "warn");
		   return;
	   }
	   
       var rowIds = $("#gridlist").jqGrid("getGridParam", "selarrrow") || [];
       if(rowIds.length <=0){
    	   $.HN.message.alert("请选择要配送的药品！", "<spring:message code="message.HN.alert.type"/>", "warn");
           return;
       }
       
       var approveCodeMap = {}; //  批号map(校验重复用)
       var invoiceNoMap = {};   //  发票号map(校验重复用)
       var list = [];    //  订单明细集合
       var drugNames = '';
       //   循环获取子表格信息
       for (var i=0; i<rowIds.length; i++) {
           rowId = rowIds[i];
           var rowdata = $("#gridlist").jqGrid('getRowData', rowId);
           var orderCode = rowdata.orderCode || ""; //  订单明细编号
           var proPrice = rowdata.proPrice || "";   //  采购价
           var drugName = rowdata.drugName || "";   //  通用名
           var proCount = rowdata.proCount || 0;    //  采购数量
           proCount = parseInt(proCount, 10);   //  十进制转数字类型

           var $subTable = $("#subTable_" + rowId); //  子表格
           var subRecords = $subTable.getGridParam("records") || 0; //  子表格记录数
           
           if (subRecords <= 0) {
        	   $.alert("请为采购价" + proPrice + "的" + drugName + "药品输入批号、数量、有效期等信息！", "warn");
        	   return;
           }
           
           var delCountSum = 0; //  同一药品不同批号的配送数量总和
           var dispatchInfoList = [];   //  某条明细多批号、发票等信息
           
           //   循环子表格的行
           for (var j=0; j<subRecords; j++) {
        	   //  获取表单对象
               var $approveCode = $subTable.find("input.approveCode:eq(" + j + ")");
               var $delCount = $subTable.find("input.delCount:eq(" + j + ")");
               var $credentialDatetime = $subTable.find("input.credentialDatetime:eq(" + j + ")");
               var $remarks = $subTable.find("input.remarks:eq(" + j + ")");
               var $confirmRemark = $subTable.find("textarea.confirmRemark:eq(" + j + ")");
               var confirmRemark = $.trim($confirmRemark.val());
        	   $confirmRemark.val(confirmRemark);
               //   获取表单值
        	   var approveCode = $.trim($approveCode.val());
        	   var delCount = $.trim($delCount.val());
        	   var credentialDatetime = $.trim($credentialDatetime.val());
        	   var remarks = $.trim($remarks.val());
        	   
        	   //  去除表单首尾空格
        	   $approveCode.val(approveCode);
        	   $delCount.val(delCount);
        	   $credentialDatetime.val(credentialDatetime);
        	   $remarks.val(remarks);
       	   if (confirmRemark.length > 0){
       		$.alert("请勿为药品 " + drugName + " 填写缺货原因！", "warn");
 		   return;
       	   }
        	   //  判断批号是否为空
        	   if (approveCode == null || approveCode == "") {
        		   $.alert("请为采购价" + proPrice + "的" + drugName + "药品输入批号！", "warn");
        		   $approveCode[0].focus();
        		   return;
        	   }
        	   
        	   //  判断批号是否重复
        	   if (approveCodeMap[approveCode] == 1) {
        		   $.alert("采购价" + proPrice + "的" + drugName + "药品批号重复！", "warn");
                   $approveCode[0].focus();
                   return;
        	   } else {
        		   approveCodeMap[approveCode] = 1;
        	   }
        	   
        	   //  判断配送数量是否为空
        	   if (delCount == null || delCount == "") {
                   $.alert("请为采购价" + proPrice + "的" + drugName + "药品输入配送数量！", "warn");
                   $delCount[0].focus();
                   return;
               }
        	   
        	   //  判断配送数量是否为正整数
        	   if (isNaN(delCount) || delCount.indexOf(".") >= 0 || parseInt(delCount, 10) <= 0) {
        		   $.alert("采购价" + proPrice + "的" + drugName + "药品配送数量不是正整数！", "warn");
                   $delCount[0].focus();
                   return;
        	   } else {
                   delCount = parseInt(delCount, 10);  //  十进制转数字类型
               }
        	   
        	   //  判断配送数量是否大于采购数量
        	   if (delCount > proCount) {
                   $.alert("采购价" + proPrice + "的" + drugName + "药品配送数量不能大于采购数量！", "warn");
                   $delCount[0].focus();
                   return;
               } else {
            	   delCountSum += delCount;
               }
        	   
        	   //  判断同一药品不同批号的配送数量总和是否大于采购数量
               if (delCountSum > proCount) {
                   $.alert("采购价" + proPrice + "的" + drugName + "药品不同批号的配送数量总和不能大于采购数量！", "warn");
                   $delCount[0].focus();
                   return;
               }
               if (credentialDatetime.length < 1) {
                   $.alert("采购价" + proPrice + "的" + drugName + "药品有效期不能为空！", "warn");
                   $credentialDatetime[0].focus();
                   return;
               }
        	   
        	 
            	//   批号、发票等信息
                   var dispatchInfo = {
                	   "approveCode" : approveCode,
                	   "delCount" : delCount,
                	   "credential" : credentialDatetime,
                	   "remarks" : remarks
                   };
               
               dispatchInfoList.push(dispatchInfo);
           }
           
           var detail = {"orderCode": orderCode, "dispatchInfoList":dispatchInfoList};   //  一条明细信息
           list.push(detail);
		   
		   if(i<3){
                drugNames+= rowdata.drugName+"<br/>"
                if(i==2)
                drugNames+='...';
                	
           }
       }
       $.HN.message.confirm("确认配送以下药品吗？<br/>"+drugNames, "<spring:message code="message.HN.alert.type"/>", "<spring:message code="message.lable.OKYES"/>").on(function(bool) {
           if (bool) {
        	   submitTyp = 1;
        	   $("#recpCode").val(getRecpCode());
               $.post("${ctx}/orderDispatch/dispatchDetail.html", {
            	   "orderPackId" : "${orderPackId}",
            	   "submitDatetime" : "${orderPack.submitDatetime}",
            	   "orderConfirmTime" : "${orderPack.orderConfirmTime}",
            	   "recpCode" : recpCode,
                   "orderDetails" : json.jsonToString(list)
               }, function(result) {
            	   submitTyp = 0;
                   if (result.success) {
                	   $.HN.message.alert("配送成功！", "", "success");
                       gridSerch();
                   } else {
                       $.HN.message.alert(result.msg, "", "error");
                   }
               }, "json");
           }
       });   
   }
   
   function queHuo(){

	   if (submitTyp == 1) {
		   $.alert("后台处理中，请勿重复操作！", "warn");
           return;
	   }
	   
	   var recpCode = $.trim($("#recpCode").val());
	   $("#recpCode").val(recpCode);
	   if (recpCode == null || recpCode == "") {
		   $.alert("请输入验收单号！", "warn");
		   return;
	   }
	   
       var rowIds = $("#gridlist").jqGrid("getGridParam", "selarrrow") || [];
       if(rowIds.length <=0){
    	   $.HN.message.alert("请选择要缺货的药品！", "<spring:message code="message.HN.alert.type"/>", "warn");
           return;
       }
       
       var approveCodeMap = {}; //  批号map(校验重复用)
       var invoiceNoMap = {};   //  发票号map(校验重复用)
       var list = [];    //  订单明细集合
       var drugNames = '';
       //   循环获取子表格信息
       for (var i=0; i<rowIds.length; i++) {
           rowId = rowIds[i];
           var rowdata = $("#gridlist").jqGrid('getRowData', rowId);
           var orderCode = rowdata.orderCode || ""; //  订单明细编号
           var proPrice = rowdata.proPrice || "";   //  采购价
           var drugName = rowdata.drugName || "";   //  通用名
           var proCount = rowdata.proCount || 0;    //  采购数量
           proCount = parseInt(proCount, 10);   //  十进制转数字类型

           var $subTable = $("#subTable_" + rowId); //  子表格
           var subRecords = $subTable.getGridParam("records") || 0; //  子表格记录数
           
           if (subRecords <= 0) {
        	   $.alert("请为采购价" + proPrice + "的" + drugName + "药品输入批号、数量、有效期等信息！", "warn");
        	   return;
           }
           
           var delCountSum = 0; //  同一药品不同批号的配送数量总和
           var dispatchInfoList = [];   //  某条明细多批号、发票等信息
           
           //   循环子表格的行
           for (var j=0; j<subRecords; j++) {
        	   //  获取表单对象
               var $confirmRemark = $subTable.find("textarea.confirmRemark:eq(" + j + ")");
               var $remarks = $subTable.find("input.remarks:eq(" + j + ")");
               //   获取表单值
        	   var confirmRemark = $.trim($confirmRemark.val());
        	   var remarks = $.trim($remarks.val());
        	   
        	   
        	   
        	   
        	   var $approveCode = $subTable.find("input.approveCode:eq(" + j + ")");
               var $delCount = $subTable.find("input.delCount:eq(" + j + ")");
               var $credentialDatetime = $subTable.find("input.credentialDatetime:eq(" + j + ")");
               //   获取表单值
        	   var approveCode = $.trim($approveCode.val());
        	   var delCount = $.trim($delCount.val());
        	   var credentialDatetime = $.trim($credentialDatetime.val());
        	   if (approveCode.length > 0){
        		   $.alert("请勿为药品 " + drugName + " 填写批次号！", "warn");
                   return;
        	   }
        	   
        	   if (delCount.length > 0){
        		   $.alert("请勿为药品 " + drugName + " 填写配送数量！", "warn");
                   return;
        	   }
        	   
        	   if (credentialDatetime.length > 0){
        		   $.alert("请勿为药品 " + drugName + " 填写有效期！", "warn");
                   return;
        	   }
        	   
        	   
        	   //  去除表单首尾空格
        	   $confirmRemark.val(confirmRemark);
        	   $remarks.val(remarks);
       	   
        	   
               if (confirmRemark.length < 1) {
                   $.alert("采购价" + proPrice + "的" + drugName + "药品缺货原因不能为空！", "warn");
                   $confirmRemark[0].focus();
                   return;
               }
        	   
        	 
            	//   批号、发票等信息
                   var dispatchInfo = {
                	   "confirmRemark" : confirmRemark,
                	   "remarks" : remarks
                   };
               
               dispatchInfoList.push(dispatchInfo);
           }
           
           var detail = {"orderCode": orderCode, "dispatchInfoList":dispatchInfoList};   //  一条明细信息
           list.push(detail);

			if(i<3){
                drugNames+= rowdata.drugName+"<br/>"
                if(i==2)
                drugNames+='...';
                	
           }
       }
       $.HN.message.confirm("确认以下药品缺货吗？<br/>"+drugNames, "<spring:message code="message.HN.alert.type"/>", "<spring:message code="message.lable.OKYES"/>").on(function(bool) {
           if (bool) {
        	   submitTyp = 1;
        	   $("#recpCode").val(getRecpCode());
               $.post("${ctx}/orderDispatch/shortDrug.html", {
            	   "orderPackId" : "${orderPackId}",
            	   "submitDatetime" : "${orderPack.submitDatetime}",
            	   "orderConfirmTime" : "${orderPack.orderConfirmTime}",
            	   "recpCode" : recpCode,
                   "orderDetails" : json.jsonToString(list)
               }, function(result) {
            	   submitTyp = 0;
                   if (result.success) {
                	   $.HN.message.alert("缺货成功！", "", "success");
                       gridSerch();
                   } else {
                       $.HN.message.alert(result.msg, "", "error");
                   }
               }, "json");
           }
       }); 
   }
   
   function exportExcel(){
		var postData = {drugCode:$.trim($("#drugCode").val()),drugName:$.trim($("#drugName").val()),compName:$.trim($("#prodComp").val()),orderPackId:"${orderPackId}"};
        $.StandardPost("${ctx}/orderDispatch/exportExcelByOrderDispatchDetail.html",postData);
   }
   </script>
</body>
</html>
